Skip to content

fix(server/grants): persist the password grant's user identity so refresh works with sessions - #5168

Merged
nabokihms merged 3 commits into
dexidp:masterfrom
santhiprakash:5166-password-grant-user-identity
Nov 17, 2026
Merged

nabokihms merged 3 commits into
dexidp:masterfrom
santhiprakash:5166-password-grant-user-identity

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Overview

With sessions enabled, every refresh of a token issued by the password grant fails with HTTP 500 ({"error":"invalid_request"}) and the server logs failed to get user identity. The same happens for any refresh token minted before sessions were turned on.

What this PR does / why we need it

Closes #5166

Only the browser flow writes a UserIdentity (server/authflow/finalize.go), so a password-grant user never has a row, and server/grants/refresh.go turns the resulting storage.ErrNotFound into a 500.

Two changes, along the lines the issue itself suggests:

  1. The password grant persists the user identity the same way finalize.go does — created on first login, claims and last-login refreshed on later ones — so its refresh tokens resolve the cached claims under sessions, without contacting the upstream connector (the behaviour TestRefreshDisconnectsUpstreamWhenSessionsEnabled pins).
  2. A missing identity answers 400 invalid_grant, not a 500. Under sessions there are no cached claims to refresh against, so a token minted before sessions were enabled (or before this change) simply cannot be honored — a grant failure the client can act on, not a server fault.

Verification — the new TestRefreshGrantPasswordFlowWithSessions issues a password-grant token with sessions enabled and refreshes it:

  • on master (a4755513): level=ERROR msg="failed to get user identity" err="not found" → expected: 200, actual: 500, body {"error":"invalid_request"}
  • on this branch: passes; TestRefreshDisconnectsUpstreamWhenSessionsEnabled (refresh does not contact upstream under sessions), TestRefreshGrantStorageBackedPasswordDB (sessions off — the connector is still consulted), and the rest of go test ./server/... all pass; golangci-lint run reports 0 issues.

Special notes for your reviewer

One existing assertion changed, deliberately: TestRefreshTokenAuthTime's "sessions enabled but user identity missing" case asserted exactly the 500 #5166 reports. It now asserts 400 / invalid_grant, matching the issue's own suggestion. If you would rather see a different error, or a connector fallback here, I'm happy to adjust — I left the fallback out on purpose since the sessions design is that refresh must not contact the upstream.

Deliberately out of scope: the grant mirrors finalize.go's identity write, but not the BlockedUntil lock check finalizeLogin runs on browser logins (the password grant never had an identity row to check). Worth a follow-up if you want the lock enforced on this path too.

…resh works with sessions

A UserIdentity row was only ever written by the browser flow's finalize
step, so a refresh token issued by the password grant — or minted before
sessions were enabled — had none. With sessions on, every refresh of such
a token failed GetUserIdentity and returned a 500.

The password grant now creates/updates the identity the same way
finalize.go does, and the refresh grant answers a missing identity with
400 invalid_grant instead of a 500: under sessions there are no cached
claims to fall back to, so the token can no longer be honored.

Signed-off-by: Santhi Prakash <b.santhiprakash@gmail.com>

@nabokihms nabokihms left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the fix and the invalid_grant change look right. One thing inline that needs fixing before merge.

Comment thread server/grants/password.go Resolved
…ogin paths

The password grant persisted the user identity the way finalizeLogin does
but not the lock check ahead of it, so a locked account could still mint
tokens. Both steps now live in one helper, useridentity.RecordLogin, called
from the password grant and from finalizeLogin so the paths cannot drift
apart again. A locked account gets access_denied from the token endpoint.

Signed-off-by: Santhi Prakash <b.santhiprakash@gmail.com>
…password grant

Matches the response shape proposed in dexidp#5176: a locked account's token
request is an invalid_grant (400), not access_denied.

Signed-off-by: Santhi Prakash <b.santhiprakash@gmail.com>

@nabokihms nabokihms left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM!

@nabokihms nabokihms added the release-note/bug-fix Release note: Bug Fixes label Nov 17, 2026
@nabokihms
nabokihms merged commit 60f28dd into dexidp:master Nov 17, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refresh returns 500 "failed to get user identity" for password grant tokens when sessions are enabled

2 participants